package com.lsa.blog.service.Impl;

import com.lsa.blog.Dao.TagRepository;

import com.lsa.blog.NotFoundException;
import com.lsa.blog.po.Tag;
import com.lsa.blog.po.Type;
import com.lsa.blog.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository repository;


    /**
     * 保存分类名称
     * @param
     * @return
     */
    @Transactional
    @Override
    public Tag saveTag(Tag Tag) {
        return repository.save(Tag);
    }

    /**
     * 根据id查找分类
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Tag getTag(Long id) {
        return repository.findById(id).orElse(null);
    }

    /**
     * 根据分类名称查找
     * @param name
     * @return
     */
    @Override
    public Tag getTagByName(String name) {
        return repository.findByName(name);
    }

    /**
     * 分页，查询全部标签
     * @param pageable
     * @return
     */
    @Transactional
    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 分页，查询全部标签
     * @param
     * @return
     */
    @Override
    public List<Tag> listTag() {
        return repository.findAll();
    }

    /**
     * 分页，根据传入的参数决定查询几个（前几个）标签
     * @param
     * @return
     */
    @Override
    public List<Tag> listTagTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC,"blogs.size");
        Pageable pageable = PageRequest.of(0,size,sort);
        return repository.findTop(pageable);
    }

    /**
     * 将标签ids[] 封装成list
     * @param ids
     * @return
     */
    private List<Long> convertToList(String ids){
        List<Long> list = new ArrayList<>();
        if(!"".equals(ids)&& ids != null){
            String[] idarray = ids.split(",");
            for (int i=0;i<idarray.length;i++){
                list.add(new Long(idarray[i]));
            }
        }
        return list;
    }

    @Override
    public List<Tag> listTag(String ids) {
        return repository.findAllById(convertToList(ids));
    }


    /**
     * 修改分类
     * @param id
     * @param Tag
     * @return
     */
    @Transactional
    @Override
    public Tag updateTag(Long id, Tag Tag) {
        Tag t = repository.findById(id).orElse(null);
        if (t == null){
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(Tag,t);
        return repository.save(t);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Transactional
    @Override
    public void deleteTag(Long id) {
        repository.deleteById(id);

    }
}
